﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Xml;

namespace DetectingKinectSencor._40_Tools
{
    public class XMLproperties
    {
        public static float offsetViewingDirection {get; set;}
        public static float secondsLookedAwayDetected { get; set; }
        public static float offsetMovementManager { get; set; }
        public static float secondsTillMovingDetected { get; set; }
        public static float offsetOpenGesture { get; set; }
        public static int milliSecondsTillOpenGestureDetected { get; set; }
        public static float offsetHandsBelowWaistline { get; set; }
        public static float secondsTillHandsBelowwastelineDetected { get; set; }
        public static int offsetSlidesChange { get; set; }
        public static int requiredAveragesToDetectVideo { get; set; }
        public static int offsetSkeletonPosition { get; set; }
        public static int offsetHipSkeletonPosition { get; set; }

        public static float thresholdImageDetected { get; set; }
        public static int secondsTillSnapshot { get; set; }
        public static float thresholdVideoDetected { get; set; }
        public static float thresholdToCalculateOffestForNewShoulderPosition { get; set; }
        public static int thresholdChangeBorderOfRectangle { get; set; }
             


        public static int framesTillSnapshot
                            {
                                get { return secondsTillSnapshot * 30; }
                            }

        static string pattern = ",";
        static string replacementDot = ".";

        static Regex replaceComma = new Regex(pattern);



        public static float additionalOffsetSmall = 0.03f;
        public static float additionalOffsetBig = 0.05f;
        public static int thresholdDistanceFromHipOldtoHipNew = 25;

        

        public static void readPropertiesFromXML()
        {
            try
            {
                using (XmlReader xmlReader = XmlReader.Create("properties.xml"))
                {
                    while (xmlReader.Read())
                    {
                        if (xmlReader.IsStartElement())
                        {
                            switch (xmlReader.Name)
                            {
                                case "offsetViewingDirection":
                                    xmlReader.Read();
                                    offsetViewingDirection = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "secondsLookedAwayDetected":
                                    xmlReader.Read();
                                    secondsLookedAwayDetected = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "offsetMovementManager":
                                    xmlReader.Read();
                                    offsetMovementManager = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "secondsTillMovingDetected":
                                    xmlReader.Read();
                                    secondsTillMovingDetected = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "offsetOpenGesture":
                                    xmlReader.Read();
                                    offsetOpenGesture = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "secondsTillOpenGestureDetected":
                                    xmlReader.Read();
                                    milliSecondsTillOpenGestureDetected = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "offsetHandsBelowWaistline":
                                    xmlReader.Read();
                                    offsetHandsBelowWaistline = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "secondsTillHandsBelowwastelineDetected":
                                    xmlReader.Read();
                                    secondsTillHandsBelowwastelineDetected = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "offsetSlidesChange":
                                    xmlReader.Read();
                                    offsetSlidesChange = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "requiredAveragesToDetectVideo":
                                    xmlReader.Read();
                                    requiredAveragesToDetectVideo = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "offsetSkeletonPosition":
                                    xmlReader.Read();
                                    offsetSkeletonPosition = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "offsetImageDetected":
                                    xmlReader.Read();
                                    thresholdImageDetected = xmlReader.ReadElementContentAsFloat();
                                    break; 
                                case "secondsTillSnapshot":
                                    xmlReader.Read();
                                    secondsTillSnapshot = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "offsetVideoDetected":
                                    xmlReader.Read();
                                    thresholdVideoDetected = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "thresholdToCalculateOffestForNewShoulderPosition":
                                    xmlReader.Read();
                                    thresholdToCalculateOffestForNewShoulderPosition = xmlReader.ReadElementContentAsFloat();
                                    break;
                                case "offsetHipSkeletonPosition":
                                    xmlReader.Read();
                                    offsetHipSkeletonPosition = xmlReader.ReadElementContentAsInt();
                                    break;
                                case "thresholdChangeBorderOfRectangle":
                                    xmlReader.Read();
                                    thresholdChangeBorderOfRectangle = xmlReader.ReadElementContentAsInt();
                                    break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        public static void writePropertiesToXML()
        {
            
            using (XmlWriter xmlWriter = XmlWriter.Create("properties.xml"))
            {
                xmlWriter.WriteStartDocument();


                xmlWriter.WriteStartElement("properties");

                    xmlWriter.WriteStartElement("offsetViewingDirection");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetViewingDirection.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("secondsLookedAwayDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(secondsLookedAwayDetected.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetMovementManager");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetMovementManager.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("secondsTillMovingDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(secondsTillMovingDetected.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetOpenGesture");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetOpenGesture.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("secondsTillOpenGestureDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(milliSecondsTillOpenGestureDetected.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetHandsBelowWaistline");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetHandsBelowWaistline.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("secondsTillHandsBelowwastelineDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(secondsTillHandsBelowwastelineDetected.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetSlidesChange");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetSlidesChange.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("requiredAveragesToDetectVideo");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(requiredAveragesToDetectVideo.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetSkeletonPosition");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetSkeletonPosition.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetImageDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(thresholdImageDetected.ToString(),replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("secondsTillSnapshot");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(secondsTillSnapshot.ToString(), replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetVideoDetected");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(thresholdVideoDetected.ToString(), replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("thresholdToCalculateOffestForNewShoulderPosition");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(thresholdToCalculateOffestForNewShoulderPosition.ToString(), replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("offsetHipSkeletonPosition");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(offsetHipSkeletonPosition.ToString(), replacementDot));
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteStartElement("thresholdChangeBorderOfRectangle");
                        xmlWriter.WriteElementString("value", replaceComma.Replace(thresholdChangeBorderOfRectangle.ToString(), replacementDot));
                    xmlWriter.WriteEndElement();

                

                xmlWriter.WriteEndElement();

                xmlWriter.WriteEndDocument();

            }
        }
    }
}
